<?php

use yii\helpers\Html;
use kartik\select2\Select2;
use app\modules\Order\models\Leader;

$this->title = $product->name . '  ' . $product->go_time;


//获取领队信息

$leaders = Leader::find()->where(['status'=>1])->all();

$lead = [];

if(!empty($leaders)){
	foreach($leaders as $v){
		$lead[$v->id] = $v->first_name.$v->last_name;
	}
}

 

?>
<?= yii\web\View::registerCssFile("/css/magic-check.css",['depends' => 'app\assets\AppAsset']) ?>
<style type="text/css">
	.order-view font {color:#9E9A9A;margin-right:10px;}
	.team{border: 1px solid #ddd;border-radius: 4px;padding-left: 15px;margin-right: 15px;height: 200px;margin-top: 15px;}
	.team .team-t{padding: 15px;height: 80px;background: #dff0d8;margin-top: 15px;line-height: 50px;text-align: center;}
	.team .team-p{margin-top: 15px;}
	.team .team-b{height: 40px;line-height: 50px;text-align: center;}
	.magic-radio + label, .magic-checkbox + label{display: inline;}

	.download{border: 1px solid #ddd;padding: 15px;border-radius: 3px;}
	.list-group-item{line-height: 30px;}

</style>

<div class="order-view">
    <h2 class="page-header">
    	 <small><?=yii::$app->params['template'][$template_id]?></small>
    	 &nbsp;&nbsp;<small>[ <?= Html::encode($this->title) ?> ]</small>
    </h2>
	<!-- div class="row" style="padding:15px;"> 
		 <a data-toggle="modal" data-target=".leader-modal"  href="/order/leader/create-new" class="btn btn-primary">创建新领队</a>	
	</div>

	<hr-->

	<input type="hidden" name="template_id" id="template_id" value="<?=$template_id?>">
	<input type="hidden" name="product_id" id="product_id" value="<?=$product->id?>">
	<input type="hidden" value="<?php echo Yii::$app->getRequest()->getCsrfToken(); ?>" name="_csrf" id="_csrf" />

	<div class="row" style="padding: 15px;">
    	<?php if(!empty($assign)) { ?>
    		<?php foreach ($assign as  $v){?>
   				<div class="col-md-3 team">
   					 <div class="team-t">
   					 		<?=$v->name?>
   					 </div>
   					 <!-- <div class="team-p">
   					 	</?php 
   					 		echo Select2::widget([
							    'name' => 'lead_'.$v->id,
							    'data' => $lead,
							    'value' => $v->leader_id,
							    'options' => [
							        'placeholder' => '选择领队',
							        'class' => 'assign_lead  lead_'.$v->id,
							        'id' => 'lead_'.$v->id,
							        'data-assign' => $v->id
							    ],
							   
							]);
   					 	?>
   					 </div> -->
   					 <div class="team-b">
   					 	<input class="magic-checkbox" type="checkbox" name="layout" id="assign_<?=$v->id?>" value="<?=$v->id?>" data-leader="<?=$v->leader_id?>">
   					 	<label for="assign_<?= $v->id?>"></label>
   					 </div>
   				</div>
    		<?php }?>


    	<?php }?>

				
	</div>

	

	<div class="row" style="text-align: center;margin-top: 30px;">
		<button type="button" class="btn btn-warning" id="create_name">生成团队名单</button>
	</div>

	<div class="row col-md-6" id="file" style="padding-top:30px;">
			
			
	</div>

	

</div>


<!-- div class="modal fade leader-modal"   role="dialog" aria-labelledby="myLargeModalLabel">
  <div class="modal-dialog " role="document">
      <div class="modal-content">
	 		
	  </div>
  </div>
</div -->

<?php app\components\JsBlock::begin() ?>
<script type="text/javascript">
	  //保存领队对应的分团	
	 $(".assign_lead").change(function(){
	        var lead_id = $(this).val();
		    var assign_id = $(this).attr("data-assign");
		   	$.get("/order/customer/save-leader", {"assign_id":assign_id,"lead_id":lead_id}, function(data){
	            if(data){
	                console.log(data);
	            }
        	});
	       
	  });

	$("#create_name").click(function(){

		var template_id = $("#template_id").val();

		var product_id = $("#product_id").val();

		var _csrf = $("#_csrf").val();

		var assign_arr = [];

		$('input:checkbox[name=layout]:checked').each(function(i){
		     var assign_id = $(this).val();
		     //var select_id = "lead_"+assign_id;
		     //var lead_id = $('#'+select_id).val();
		     var lead_id = $(this).attr("data-leader");
		     assign_arr[i] = {'assign_id':assign_id,'lead_id':lead_id};
      	});

      	if(assign_arr.length === 0){
      		alert("没有选择团队!");
      		return false;
      	}

      	$.ajax({
		   type: "POST",
		   async:false, 
		   url: "/order/customer/excel",
		   data: {"_csrf":_csrf,"template_id":template_id,"product_id":product_id,"assign_arr":assign_arr},
		   success: function(msg){
		       var res = $.parseJSON(msg);
		       if(res.status == 0){
		       	   alert("生成团队名单失败");
		       	   return false;
		       }else if(res.status == 1){

		       	var href = encodeURI("/order/customer/download-file?name="+res.data.down_name+"&url="+res.data.down_url);
		       	window.location.href = href;
		       		
		       	// 	$("#file").empty();
		       	// 	var html = "";
		       	// 	html = "<div class='panel panel-default'>"+
		  						// "<div class='panel-heading'>"+
				    		// 	"<h3 class='panel-title'>生成团队名单文件列表</h3>"+
				  				// "</div><div class='panel-body'>"+
				    		// 	"<ul class='list-group'>"; 
		       	// 	$.each(res.data,function(i,item){
		       	// 		var href = "/order/customer/download-file?name="+item.down_name+"&url="+item.down_url;
		       	// 		html += "<li class='list-group-item list-group-item-info'>团队名称："+item.assign_name+" 领队姓名："+item.leader_name+" <a class='btn btn-success btn-sm pull-right download_file' href="+href+" >下载EXCEL文件</a></li>";
		       	// 	});

		       	// 	html += "</ul></div></div>";

		       	// 	$("#file").append(html);
					
		       }

		   }
		});

	});

	

</script>
<?php app\components\JsBlock::end() ?>
